package tyk.Dao;

import com.alibaba.druid.pool.DruidDataSource;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/***
 *@title BaseDao
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/5 21:13
 **/
public class BaseDao {
//    Todo:封装jdbc工具类
//    查询操作未封装
    private static DruidDataSource dataSource;
    private static ThreadLocal threadLocal;
    static {
        threadLocal =new ThreadLocal();
        dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/javaweb");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        System.out.println("static");
    }

    //增 删 改
    public static int executeUpdate(String sql,Object ... par) throws SQLException {
        Connection connection=null;
        connection= (Connection) threadLocal.get();
        if(connection==null){
            connection=dataSource.getConnection();
            //threadLocal.set(connection);
        }
        //System.out.println("33333333333333333");

        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        for(int i=1;i<=par.length;++i) preparedStatement.setObject(i,par[i-1]);
        int rows=preparedStatement.executeUpdate();
        preparedStatement.close();
        if(connection.getAutoCommit()) connection.close();
        return rows;
    }

    //select for one
    public static int executeQuery(String sql,Object ... params) throws SQLException {
        int rows=0;
        Connection connection =null;
        connection= (Connection) threadLocal.get();
        if(connection==null){
            connection=dataSource.getConnection();
        }
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        for (int i=1;i<=params.length;++i){
            preparedStatement.setObject(i,params[i-1]);
        }
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()) rows=1;
        resultSet.close();
        preparedStatement.close();
        if(connection.getAutoCommit()) connection.close();
        return rows;
    }


    public <T> List<T> executeQuery(Class<T> clazz,String sql, Object ... params) throws SQLException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        Connection connection =null;
        connection= (Connection) threadLocal.get();
        if(connection==null){
            connection=dataSource.getConnection();
        }
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        for (int i=1;i<=params.length;++i){
            preparedStatement.setObject(i,params[i-1]);
        }
        ResultSet resultSet=preparedStatement.executeQuery();
        List<T> list =new ArrayList<>();

        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount=metaData.getColumnCount();
        while (resultSet.next()){
            T t=clazz.newInstance();
            for(int i=1;i<=columnCount;++i){
                Object value=resultSet.getObject(i);
                String propertyName=metaData.getColumnLabel(i);
                Field field=clazz.getDeclaredField(propertyName);
                field.setAccessible(true);
                field.set(t,value);
            }
            list.add(t);
        }
        resultSet.close();
        preparedStatement.close();
        if(connection.getAutoCommit()) connection.close();
        return list;
    }


}
